﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Styx;
using Styx.WoWInternals;
using Styx.Helpers;

namespace SwInYUtility
{
    internal static class HasInvited
    {
        private static List<Player> _AlreadyInvited = new List<Player>();
        private static string _SavePath = "";


        internal static void Init()
        {
           // Set our save path
            _SavePath = string.Format("{0}\\{1}_InvitedList.txt", GuildInviter._FolderLocation, StyxWoW.Me.Name);

            // Error catch
            if (!File.Exists(_SavePath)) return;

            StreamReader Reader = new StreamReader(_SavePath);
            while (Reader.Peek() >= 0)
            {
                string PlayerName = Reader.ReadLine().Trim();
                if (!IsKnown(PlayerName))
                    _AlreadyInvited.Add(new Player(PlayerName));
            }


            Reader.Close();
            Log.Write(Log.eLogType.GuildInviter, "Ignoring {0} Players Already Invited!", _AlreadyInvited.Count());
        }

        internal static void Add(string Name)
        {
            _AlreadyInvited.Add(new Player(Name));

            // Write to file
            StreamWriter Writer = new StreamWriter(_SavePath, true);
            Writer.WriteLine(Name);
            Writer.Close();
        }

        /// <summary>
        /// If the player is already known
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        internal static bool IsKnown(string Name)
        {
            int PlayerCount = _AlreadyInvited.Where(e => e.Name == Name).Count();
            if (PlayerCount >= 1) return true;

            return false;
        }
    }
}
